import { Model } from 'sequelize';

import getIPAddress from '../../util/network';
import { PORT } from '../../../constants/front';
import { DOMAIN } from '../../../constants/api';

const { NODE_ENV } = process.env;

/** 本地或测试环境的 QRCODE */
const LOCAL_QR_CODE = `http://${getIPAddress()}:${PORT}/questionAnswer/home`;
/** 开发环境的 QRCODE */
const PROD_QR_CODE = `https://${DOMAIN}/questionAnswer/home`;

function modelNumToBoolean(model: QuestionnaireModel, field: string) {
  const rawValue = model.getDataValue(field);
  return Boolean(rawValue);
}
class QuestionnaireModel extends Model {
}

module.exports = (app) => {
  const {
    STRING, INTEGER, DATE, VIRTUAL
  } = app.Sequelize;

  const Questionnaire = app.model.define('Questionnaire', {
    id: { type: INTEGER, primaryKey: true, autoIncrement: true },
    uuid: { type: STRING(36), unique: true },
    // 是否开启问卷
    open: {
      type: INTEGER,
      defaultValue: 0,
      get(this: QuestionnaireModel) {
        const rawValue = this.getDataValue('open');
        return Boolean(rawValue);
      }
    },
    name: { type: STRING(100) },
    FKTemplate: { type: INTEGER, field: 'fk_template' },
    userNameVisible: {
      type: INTEGER,
      field: 'user_name_visible',
      defaultValue: 0,
      get(this: QuestionnaireModel) {
        return modelNumToBoolean(this, 'userNameVisible');
      }
    },
    userNumVisible: {
      type: INTEGER,
      field: 'user_num_visible',
      defaultValue: 0,
      get(this: QuestionnaireModel) {
        return modelNumToBoolean(this, 'userNumVisible');
      }
    },
    // 工作单位选择设置是否开启
    workUnitVisible: {
      type: INTEGER,
      defaultValue: 0,
      field: 'work_unit_visible',
      get(this: QuestionnaireModel) {
        return modelNumToBoolean(this, 'workUnitVisible');
      }
    },
    // 附属单位选择设置是否开启
    affiliatedVisible: {
      type: INTEGER,
      defaultValue: 0,
      field: 'affiliated_visible',
      get(this: QuestionnaireModel) {
        return modelNumToBoolean(this, 'affiliatedVisible');
      }
    },
    // 工作职位选择设置是否开启
    jobPositionVisible: {
      type: INTEGER,
      defaultValue: 0,
      field: 'job_position_visible',
      get(this: QuestionnaireModel) {
        return modelNumToBoolean(this, 'jobPositionVisible');
      }
    },
    // 工作岗位位选择设置是否开启
    operatingPostVisible: {
      type: INTEGER,
      defaultValue: 0,
      field: 'operating_post_visible',
      get(this: QuestionnaireModel) {
        return modelNumToBoolean(this, 'operatingPostVisible');
      }
    },
    QRCode: {
      type: VIRTUAL,
      get(this: QuestionnaireModel) {
        const uuid = this.getDataValue('uuid');

        if (NODE_ENV === 'development' || NODE_ENV === 'test') {
          return `${LOCAL_QR_CODE}/${uuid}`;
        }

        if (NODE_ENV === 'production') {
          return `${PROD_QR_CODE}/${uuid}`;
        }
        return '';
      },
    },
    createdAt: { type: DATE, field: 'created_at' },
    updatedAt: { type: DATE, field: 'updated_at' },
  }, {
    tableName: 'questionnaires'
  });

  Questionnaire.associate = () => {
    app.model.Questionnaire.belongsTo(app.model.Template, {
      foreignKey: 'fk_template',
      targetKey: 'id',
    });

    app.model.Questionnaire.belongsToMany(app.model.User, {
      through: app.model.MergeUserQuestionnaire,
      foreignKey: 'fk_questionnaire',
      targetKey: 'id',
    });

    app.model.Questionnaire.hasMany(app.model.OperatingPost, {
      as: 'operatingPost',
      foreignKey: 'fk_questionnaire',
      sourceKey: 'id',
    });

    app.model.Questionnaire.hasMany(app.model.WorkUnit, {
      as: 'workUnit',
      foreignKey: 'fk_questionnaire',
      sourceKey: 'id',
    });

    app.model.Questionnaire.hasMany(app.model.JobPosition, {
      as: 'jobPosition',
      foreignKey: 'fk_questionnaire',
      sourceKey: 'id',
    });

    app.model.Questionnaire.hasMany(app.model.AffiliatedUnit, {
      as: 'affiliatedUnit',
      foreignKey: 'fk_questionnaire',
      sourceKey: 'id',
    });
  };

  return Questionnaire;
};
